import pygame.sprite

from actor import WalkDir

"""
子弹
"""


class Bullet(pygame.sprite.Sprite):
    def __init__(self, x, y, image):
        """
        子弹类参数初始化
        :param x: x坐标
        :param y: y坐标
        :param image: 图像类
        """
        super(Bullet, self).__init__()
        self.pos_x = x
        self.pos_y = y
        self.image = image
        x, y, w, h = self.image.get_rect()
        self.rect = pygame.Rect(self.pos_x, self.pos_y, w, h)
        self.Dir = None

    def draw(self, surface, view_x, view_y):
        """
        子弹图片绘制
        :param surface:
        :param view_x:
        :param view_y:
        :return:
        """
        surface.blit(self.image, (self.pos_x - view_x, self.pos_y - view_y))

    def run(self):
        """
        子弹动态显示
        :return:
        """
        if self.Dir == WalkDir.left:
            self.pos_x -= 20
        elif self.Dir == WalkDir.up:
            self.pos_y -= 20
        elif self.Dir == WalkDir.right:
            self.pos_x += 20
        else:
            self.pos_y += 20
        self.update_rect()

    def do_kill(self):
        """
        判断子弹是否越界
        :return:
        """
        if self.pos_y < 0 or self.pos_y > 1200 or self.pos_x < 0 or self.pos_y >= 2800:
            return True
        else:
            return False

    def set_dir(self, cur_dir):
        """
        设置位置
        :param cur_dir:当前方法枚举变量
        :return:
        """
        self.Dir = cur_dir

    def update_rect(self):
        """
        更新矩形边框
        :return:
        """
        x, y, w, h = self.image.get_rect()
        self.rect = pygame.Rect(self.pos_x - 25, self.pos_y + 20, w, h)
